package com.commonmodule.commonmodule.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import org.json.JSONObject;

/**
 * @author zxb
 * @version 1.0
 * @date 2024-11-19 10:52
 */
public class AliyunUtil {


    // 发送验证码
    public static String note(String phone) {

        Config config = new Config()
                .setAccessKeyId("LTAI5tRJzM4DbnUndUa6WmM3") // 访问密钥
                .setAccessKeySecret("24F2awj9qKOodGMaqXQaUUsqVvPmKI");
        config.endpoint = "dysmsapi.aliyuncs.com"; // 访问端点  通用

        // 验证码内容
        String code = "8888";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);

        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("八九食")  // 签名
                .setTemplateCode("SMS_290981424")  // 模板
                .setPhoneNumbers(phone)  // 手机号
                .setTemplateParam(jsonObject.toString());  // 发送内容
        try {
            Client client = new Client(config);
            SendSmsResponseBody body = client
                    .sendSms(sendSmsRequest)  // 发送验证码
                    .getBody();  // 获取响应
            if (!"OK".equals(body.getCode())) {
                System.out.println("失败");
                return null;
            }
            return code;
        } catch (Exception e) {
            System.out.println("发送失败：" + e.getMessage());
            return null;
        }
    }


}
